module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", oldMsg = "";
    function isGroup() {
        return document.querySelectorAll('#ava_chat_box_view .a-child')?.length > 1;
    }
    async function getChatId(times = 0) {
        let titleEl = document.querySelector("#header .threadChat__title .header-title");
        let id = titleEl?.textContent || '';
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    function simulateScroll(element, deltaX = 0, deltaY = 100) {
        const event = new WheelEvent('wheel', {
            deltaX: deltaX,
            deltaY: deltaY,
            bubbles: true,
            cancelable: true,
            view: window
        });
        element.dispatchEvent(event);
    }
    return {
        config: {
            friendList: '#conversationListId,.ReactVirtualized__Grid.ReactVirtualized__List,#sidebarNav .todo-view',
            chat: '#container main',
            input: 'div.chat-input-content>div.rich-input',
            msgBox: '#messageViewScroll',
            inMsg: '.chat-item .chat-message:not(.me) .card .overflow-hidden:not(.trans-inited)',
            outMsg: '.chat-item .chat-message.me .card .overflow-hidden:not(.trans-inited)',
            sendBtn: '#chatInput .normal-buttons-group div[data-translate-title="STR_SEND"]',
            setting: "#chat-box-bar-id>div.left-chat-box-bar",
            tips: {
                in: 'header .threadChat__title',
                out: '#chat-box-input-container-id'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: 'header .threadChat__title .over-hidden',
                // left: ""
            },
            placeholder: {
                selector: "div.chat-input-content>div.rich-input",
                attr: "placeholder",
                delay: 200,
                update: true
            },
            inputType: "input"
        },
        init() {
            let btn;
            setInterval(() => {
                this.scrollerToBottom();
                btn = document.querySelector('#messageView div.btn-jump.btn-jump-new-msg');
                btn?.click();
            }, 1000);
        },
        async scrollerToBottom() {
            let scrollEl = document.querySelector('#messageViewContainer>div:nth-child(1)');
            simulateScroll(scrollEl, 0, 100);
        },
        getUnread() {
            let unreads = document.querySelectorAll('.msg-item .z-noti-badge__content');
            return unreads?.length || 0;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getChatTitle() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve(chatId);
            });
        },
        getChatId() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve(chatId);
            });
        },
        getText(msgBox) {
            return msgBox?.textContent || "";
        },
        getIptMsg(inputEl) {
            if (!inputEl) return "";
            if (!inputEl.querySelector('.emoji-sizer.emoji-outer')) return inputEl.textContent;
            let childrens = inputEl.childNodes;
            let texts = [];
            childrens.forEach(item => {
                let childNodes = item.childNodes;
                childNodes.forEach((op) => {
                    if (op.className?.includes('emoji-sizer emoji-outer')) {
                    } else {
                        texts.push(op.textContent);
                    }
                });
                texts.push("\n");
            });
            return texts.join("\n");
        },
        getUserInfo() {
            try {
                let info = JSON.parse(localStorage.getItem("sh_z_recentin") || "{}");
                if (!info) return null;
                let userName = info?.displayName || "";
                let avatar = info?.avatar || "";
                let account = info?.phoneNumber || "";
                if (avatar?.includes("default")) avatar = "";
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    }
};